package com.probox.modules.controller.centre;

import cn.hutool.core.date.DateUtil;
import com.probox.common.core.enums.ProductOptType;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.service.centre.IOrderInfoService;
import com.probox.common.entity.service.centre.IProductCountService;
import com.probox.common.entity.vo.centre.WarningDataVo;
import com.probox.common.entity.vo.centre.map.ProductAxisMap;
import com.probox.common.entity.vo.centre.order.OrderQueryVo;
import com.probox.common.entity.vo.centre.product.ProductDataVo;
import com.probox.common.entity.vo.centre.product.ProductInfoQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Api(value = "数据中心模块",tags = {"商品数据接口"})
@RestController
@RequestMapping("/centre-data/count/product")
public class CentreProductCountController extends BaseController {
    @Autowired
    private IProductCountService productCountService;

    @Autowired
    private IOrderInfoService orderInfoService;

    @ApiOperation(value = "获取商品预警数据")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = WarningDataVo.class)})
    @GetMapping("/getProductWarningData")
    public AjaxResult getProductWarningData(OrderQueryVo orderQueryVo){
        WarningDataVo productWarningData = productCountService.getProductWarningData(orderQueryVo);
        return AjaxResult.success(productWarningData);
    }

    @ApiOperation(value = "获取商品数据汇总")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProductDataVo.class)})
    @GetMapping("/getProductData/{startTime}/{endTime}")
    public AjaxResult getProductData(@PathVariable("startTime") Date startTime, @PathVariable("endTime") Date endTime){
        if (endTime!=null){
            endTime = DateUtil.endOfDay(endTime);
        }

        return AjaxResult.success(productCountService.selectProductData(startTime,endTime));
    }

    @ApiOperation(value = "获取当天商品数据汇总")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProductDataVo.class)})
    @GetMapping("/getProductData/{currentTime}")
    public AjaxResult getTodayProductData(){
        return AjaxResult.success(productCountService.selectTodayProductData());
    }

    @ApiOperation(value = "获取近30天数据曲线图")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProductAxisMap.class)})
    @GetMapping("/getProductDataMap/{type}")
    public AjaxResult getProductDataMap(@PathVariable("type")String type){
        ProductAxisMap productDataMap = new ProductAxisMap();
        if (ProductOptType.repurchaseRate.getKey().equals(type)) {
            productDataMap = productCountService.getProductDataRepurchaseRateMap(type);
        }else if (ProductOptType.paymentConversionRate.getKey().equals(type)) {
            productDataMap = productCountService.getPaymentConversionRateSumMap(type);
        }else{
            productDataMap = productCountService.getProductDataMap(type);
        }

        return AjaxResult.success(productDataMap);
    }

    @ApiOperation(value = "获取商品明细数据")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProductDataVo.class)})
    @GetMapping("/getProductDataDetail")
    public TableDataInfo getProductDataDetail(ProductInfoQuery productInfo){
        startPage();
        List<ProductDataVo> list = productCountService.productQueryList(productInfo);
        return getDataTable(list);
    }


    @ApiOperation(value = "获取商品比较上周涨跌榜")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProductDataVo.class)})
    @GetMapping("/getRanking/{number}/{range}")
    public TableDataInfo getRanking(@PathVariable("number") Long number , @PathVariable("range") Long range ){
        ProductInfoQuery productInfo = new ProductInfoQuery();
        productInfo.setNumber(number);
        productInfo.setRange(range);
        startPage();
        List<ProductDataVo> list = productCountService.getRanking(productInfo);
        return getDataTable(list);
    }



}
